package com.dongjiantong.user.serviceImpl;


import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.user.manager.OrgAdminManager;
import com.dongjiantong.user.mapper.RegionAccountMapper;
import com.dongjiantong.user.model.RegionAccount;
import com.dongjiantong.user.service.RegionAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by cao on 2017/11/10.
 *
 * @author
 */
@Slf4j
@Service(version = "1.0.0")
public class RegionAccountServiceImpl extends ServiceMybatis<RegionAccount> implements RegionAccountService {

    @Resource
    private RegionAccountMapper regionAccountMapper;

    @Autowired
    private OrgAdminManager orgAdminManager;


    private List<Map<String, Object>> queryTreeRegionAccount(long pid) {
        List<RegionAccount> childTreeNodes = queryTreeNode(pid);

        List<Map<String, Object>> node = new ArrayList<Map<String, Object>>();

        if (childTreeNodes.size() > 0) {
            for (RegionAccount ra : childTreeNodes) {

                Map<String, Object> childArray = new LinkedHashMap<String, Object>();

                if (ra.getParentId() == pid) {
                    childArray.put("label", ra.getAreaName());
                    childArray.put("id", ra.getId());
                    childArray.put("level", ra.getLevel());

                    childArray.put("children", queryTreeRegionAccount(ra.getId()));

                    node.add(childArray);

                }

            }

        }
        return node;
    }

    @Override
    public List<Map<String, Object>> regionAccountList() {
        return queryTreeRegionAccount(0L);
    }

    @Override
    public List<RegionAccount> getAllList() {
        return regionAccountMapper.selectAll();
    }

    @Override
    public RegionAccount selectByStreet(Integer provinceId, Integer cityId, Integer regionId, Integer streetId) {
        RegionAccount regionAccount = new RegionAccount();

        regionAccount.setId(null);
        regionAccount.setParentId(null);
        regionAccount.setLevel(4);
        regionAccount.setProvinceId(provinceId);
        regionAccount.setCityId(cityId);
        regionAccount.setRegionId(regionId);
        regionAccount.setStreetId(streetId);
        return regionAccountMapper.selectOne(regionAccount);
    }

    @Override
    public List<RegionAccount> queryTreeNode(Long pid) {
        Example example = new Example(RegionAccount.class);

        //这里给出的条件查询为parentId=100
        example.createCriteria().andEqualTo("parentId", pid);
        return regionAccountMapper.selectByExample(example);
    }

    @Override
    public RegionAccount getRegionAccount(Long id) {
        return regionAccountMapper.selectByPrimaryKey(id);
    }


    @Override
    public int createRegionAccount(RegionAccount regionAccount, String Province, String City, String Region, String Street) {

        Long parentId = 0L;
        Integer provinceId = regionAccount.getProvinceId();
        Integer cityId = regionAccount.getCityId();
        Integer regionId = regionAccount.getRegionId();
        Integer streetId = regionAccount.getStreetId();

        Map<String, Object> area = new HashMap<String, Object>();
        area.put("province", Province);
        area.put("provinceId", provinceId);
        area.put("city", City);
        area.put("cityId", cityId);
        area.put("region", Region);
        area.put("regionId", regionId);
        area.put("street", Street);
        area.put("streetId", streetId);

        //判断省是否存在
        if (provinceId > 0) {

            Example examplePro = new Example(RegionAccount.class);
            Example.Criteria criteriaPro = examplePro.createCriteria();

            criteriaPro.andEqualTo("provinceId", provinceId);
            criteriaPro.andEqualTo("parentId", 0L);
            criteriaPro.andEqualTo("level", 1);

            List<RegionAccount> provList = this.mapper.selectByExample(examplePro);
            if (provList.size() == 0) {

                RegionAccount regionAccountPro = new RegionAccount();

                regionAccountPro.setAreaName(Province);
                regionAccountPro.setAreaCode(provinceId);
                regionAccountPro.setParentId(0L);
                regionAccountPro.setLevel(1);
                regionAccountPro.setProvinceId(provinceId);
                regionAccountPro.setCityId(0);
                regionAccountPro.setRegionId(0);
                regionAccountPro.setStreetId(0);
                regionAccountPro.setCreatedId(regionAccount.getCreatedId());
                regionAccountPro.setCreatedAt(new Date());

                this.mapper.insert(regionAccountPro);
                parentId = regionAccountPro.getId();
            } else {
                parentId = provList.get(0).getId();
            }
        }

        //判断市是否存在
        if (cityId > 0) {

            Example exampleCity = new Example(RegionAccount.class);
            Example.Criteria criteriaCity = exampleCity.createCriteria();

            criteriaCity.andEqualTo("provinceId", provinceId);
            criteriaCity.andEqualTo("cityId", cityId);
            criteriaCity.andEqualTo("level", 2);

            List<RegionAccount> cityList = this.mapper.selectByExample(exampleCity);
            if (cityList.size() == 0) {
                RegionAccount regionAccountCity = new RegionAccount();

                regionAccountCity.setAreaName(City);
                regionAccountCity.setAreaCode(cityId);
                regionAccountCity.setParentId(parentId);
                regionAccountCity.setLevel(2);
                regionAccountCity.setProvinceId(provinceId);
                regionAccountCity.setCityId(cityId);
                regionAccountCity.setRegionId(0);
                regionAccountCity.setStreetId(0);
                regionAccountCity.setCreatedId(regionAccount.getCreatedId());
                regionAccountCity.setCreatedAt(new Date());

                this.mapper.insert(regionAccountCity);
                parentId = regionAccountCity.getId();

                //建立系统市管理员

                orgAdminManager.createOrgAdmin(parentId, area, City);

            } else {
                parentId = cityList.get(0).getId();
            }
        }

        //判断县是否存在
        if (regionId > 0) {
            Example exampleRegion = new Example(RegionAccount.class);
            Example.Criteria criteriaRegion = exampleRegion.createCriteria();

            criteriaRegion.andEqualTo("provinceId", provinceId);
            criteriaRegion.andEqualTo("cityId", cityId);
            criteriaRegion.andEqualTo("regionId", regionId);
            criteriaRegion.andEqualTo("level", 3);
            List<RegionAccount> regionList = this.mapper.selectByExample(exampleRegion);

            if (regionList.size() == 0) {
                RegionAccount regionAccountRegion = new RegionAccount();

                regionAccountRegion.setAreaName(Region);
                regionAccountRegion.setAreaCode(regionId);
                regionAccountRegion.setParentId(parentId);
                regionAccountRegion.setLevel(3);
                regionAccountRegion.setProvinceId(provinceId);
                regionAccountRegion.setCityId(cityId);
                regionAccountRegion.setRegionId(regionId);
                regionAccountRegion.setStreetId(0);
                regionAccountRegion.setCreatedId(regionAccount.getCreatedId());
                regionAccountRegion.setCreatedAt(new Date());

                this.mapper.insert(regionAccountRegion);
                parentId = regionAccountRegion.getId();

                //建立系统县管理员
                orgAdminManager.createOrgAdmin(parentId, area, Region);
            } else {
                parentId = regionList.get(0).getId();
            }
        }


        //判断镇是否存在
        if (streetId > 0) {

            Example exampleStreet = new Example(RegionAccount.class);
            Example.Criteria criteriaStreet = exampleStreet.createCriteria();


            criteriaStreet.andEqualTo("provinceId", provinceId);
            criteriaStreet.andEqualTo("cityId", cityId);
            criteriaStreet.andEqualTo("regionId", regionId);
            criteriaStreet.andEqualTo("streetId", streetId);
            criteriaStreet.andEqualTo("level", 4);
            List<RegionAccount> regionList = this.mapper.selectByExample(exampleStreet);


            if (regionList.size() == 0) {
                RegionAccount regionAccountStreet = new RegionAccount();

                regionAccountStreet.setAreaName(Street);
                regionAccountStreet.setAreaCode(streetId);
                regionAccountStreet.setParentId(parentId);
                regionAccountStreet.setLevel(4);
                regionAccountStreet.setProvinceId(provinceId);
                regionAccountStreet.setCityId(cityId);
                regionAccountStreet.setRegionId(regionId);
                regionAccountStreet.setStreetId(streetId);
                regionAccountStreet.setCreatedId(regionAccount.getCreatedId());
                regionAccountStreet.setCreatedAt(new Date());

                this.mapper.insert(regionAccountStreet);
                parentId = regionAccountStreet.getId();

                //建立系统镇管理员
                orgAdminManager.createOrgAdmin(parentId, area, Street);
            }
        }

        return 1;

    }

}
